﻿using Aribaccio.Core;
using log4net.Config;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows;

namespace BDPAutodetect.Simulator
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// 应用程序启动[重写]
        /// </summary>
        /// <param name="e"></param>
        protected override void OnStartup(StartupEventArgs e)
        {
            //获取欲启动进程名
            string strProcessName = Process.GetCurrentProcess().ProcessName;
            //检查进程是否已经启动，已经启动则显示报错信息退出程序。
            if (Process.GetProcessesByName(strProcessName).Length > 1)
            {
                MessageBox.Show("程序已启动运行中，请先完全退出再运行！", "系统错误", MessageBoxButton.OK, MessageBoxImage.Hand, MessageBoxResult.OK, (MessageBoxOptions)0);
                Environment.Exit(0);
            }
            //注册 Encoding
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            //注册log4net配置文件。
            XmlConfigurator.Configure(new FileInfo("Config/Loggercfg.config"));
            //应用程序启动
            base.OnStartup(e);
            //先启动容器并注册服务
            AppStartup.Instance.StartupServices();
            //再启动日志服务
            AppStartup.Instance.StartupLog();
            //再初始化服务
            AppStartup.Instance.InitializeService();
            //再构建注册构造者服务后，初始化加载窗体          
            var window = ContainerManager.Instance.GetContainerServiceRequired<MainWindow>();
            if (window != null) window.Show();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnExit(ExitEventArgs e)
        {
            Application.Current.Shutdown();
            Environment.Exit(0);
            base.OnExit(e);
        }
    }
}
